package com.example.shop_2302.order

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.param.ParamCreateOrder
import com.example.shop_2302.R
import com.example.shop_2302.adapter.PreOrderAdapter
import com.example.shop_2302.databinding.ActivityCreateOrderBinding

@Route(path = "/app/CreateOrderActivity")
class CreateOrderActivity : BaseActivity<ActivityCreateOrderBinding,OrderViewModel>() {
    lateinit var preOrderAdapter: PreOrderAdapter

    @Autowired
    @JvmField
    var preOrderNo = ""

    var addressId = 0
    override fun initView() {
        ARouter.getInstance().inject(this)
        preOrderAdapter = PreOrderAdapter()
        binding.rvPreorder.adapter = preOrderAdapter
        binding.rvPreorder.layoutManager = LinearLayoutManager(this)

        viewModel.getPreOrder(preOrderNo)

        //点击创建订单，调佣创建订单方法
        binding.tvCreateOrder.setOnClickListener {
            val paramCreateOrder = ParamCreateOrder()
            paramCreateOrder.preOrderNo = preOrderNo//预下单编号
            paramCreateOrder.addressId = addressId//配送地址id
            viewModel.createOrder(paramCreateOrder)
        }
    }

    override fun initData() {
        viewModel.getSuccess.observe(this){
            if(it.code==200){
                //商品列表
                preOrderAdapter.data.clear()
                preOrderAdapter.data.addAll(it.data.orderInfoVo.orderDetailList)
                preOrderAdapter.notifyDataSetChanged()
                //个人信息
                binding.tvUser.text = "${it.data.orderInfoVo.realName}  ${it.data.orderInfoVo.phone}"
                //地址
                binding.tvAddress.text = "${it.data.orderInfoVo.province}${it.data.orderInfoVo.city}${it.data.orderInfoVo.district}${it.data.orderInfoVo.detail}"
                binding.tvCount.text = "共${it.data.orderInfoVo.orderProNum}种商品"
                binding.tvTotal.text = "合计：${it.data.orderInfoVo.payFee}"

                addressId = it.data.orderInfoVo.addressId//给配送地址id赋值
            }
        }
        viewModel.createSuccess.observe(this){
            //ToastUtils.showLong(it.message)
            if(it.code==200){
                //生成一个未支付的订单
                ARouter.getInstance().build("/app/PayActivity").withString("orderNo",it.data.orderNo).navigation()
            }
        }
    }

    override fun getViewModel(): Class<OrderViewModel>  = OrderViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_create_order

}